home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / ab20 / ab20_archive / text / cmanual.lzh / ACM2.lzh / Requesters / Example9.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  21KB  |  535 lines

  1. /* Example9                                                             */
  2. /* This program will open a normal window which is connected to the     */
  3. /* Workbench Screen. The window will use all System Gadgets, and will   */
  4. /* close first when the user has selected the System gadget Close       */
  5. /* window. Inside the window we have activated an Application requester */
  6. /* with three connecting gadgets. Two are Boolean gadgets ("OK and      */
  7. /* "CANCEL"), and one is a Proportional gadget.                             */
  8.  
  9.  
  10.  
  11. #include <intuition/intuition.h>
  12.  
  13.  
  14.  
  15. struct IntuitionBase *IntuitionBase;
  16.  
  17.  
  18.  
  19. /*****************************************/
  20. /* THE PROPORTIONAL GADGET's STRUCTURES: */
  21. /*****************************************/
  22.  
  23. /* The IntuiText structure for the proportional gadget: */
  24. struct IntuiText prop_text=
  25. {
  26.   1,         /* FrontPen, colour register 1. */
  27.   0,         /* BackPen, colour register 0. */
  28.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  29.              /* change the background. */ 
  30.   -65, 2,    /* LeftEdge, TopEdge. */
  31.   NULL,      /* ITextFont, use default font. */
  32.   "Colour:", /* IText, the text that will be printed. */
  33.   NULL,      /* NextText, no other IntuiText structures. */
  34. };
  35.  
  36.  
  37. /* We need to declare an Image structure for the knob, but since */
  38. /* Intuition will take care of the size etc of the knob, we do not need */
  39. /* to initialize the Image structure: */
  40. struct Image prop_image;
  41.  
  42.  
  43. struct PropInfo prop_info=
  44. {
  45.   FREEHORIZ|      /* Flags, the knob should be moved horizontally, and */
  46.   AUTOKNOB,       /* Intuition should take care of the knob image. */
  47.   0,              /* HorizPot, start position of the knob. */
  48.   0,              /* VertPot, 0 since we will not move the knob hor. */
  49.   MAXBODY * 1/16, /* HorizBody, 16 steps. */
  50.   0,              /* VertBody, 0 since we will not move the knob hor. */
  51.  
  52.   /* These variables are initialized and maintained by Intuition: */
  53.  
  54.   0,              /* CWidth */
  55.   0,              /* CHeight */
  56.   0, 0,           /* HPotRes, VPotRes */
  57.   0,              /* LeftBorder */
  58.   0               /* TopBorder */
  59. };
  60.  
  61.  
  62. struct Gadget prop_gadget=
  63. {
  64.   NULL,              /* NextGadget, no more gadgets in the list. */
  65.   80,                /* LeftEdge, 80 pixels out. */
  66.   30,                /* TopEdge, 30 lines down. */
  67.   189,               /* Width, 189 pixels wide. */
  68.   12,                /* Height, 12 pixels lines heigh. */
  69.   GADGHCOMP,         /* Flags, complement the colours. */
  70.   GADGIMMEDIATE|     /* Activation, our program will recieve a message */
  71.   RELVERIFY,         /* when the user has selected this gadget, and when */
  72.                      /* the user has released it. */ 
  73.   PROPGADGET,        /* GadgetType, a Proportional gadget. */
  74.   (APTR) &prop_image,/* GadgetRender, a pointer to our Image structure. */
  75.                      /* (Intuition will take care of the knob image) */
  76.                      /* (See chapter 3 GRAPHICS for more information) */
  77.   NULL,              /* SelectRender, NULL since we do not supply the */
  78.                      /* gadget with an alternative image. */
  79.   &prop_text,        /* GadgetText, colour. */
  80.   NULL,              /* MutualExclude, no mutual exclude. */
  81.   (APTR) &prop_info, /* SpecialInfo, pointer to a PropInfo structure. */
  82.   0,                 /* GadgetID, no id. */
  83.   NULL               /* UserData, no user data connected to the gadget. */
  84. };
  85.  
  86.  
  87.  
  88. /*******************************/
  89. /* THE OK GADGET's STRUCTURES: */
  90. /*******************************/
  91.  
  92. /* The coordinates for the OK box: */
  93. SHORT ok_border_points[]=
  94. {
  95.    0,  0, /* Start at position (0,0) */
  96.   22,  0, /* Draw a line to the right to position (22,0) */
  97.   22, 10, /* Draw a line down to position (22,10) */
  98.    0, 10, /* Draw a line to the left to position (0,10) */
  99.    0,  0  /* Finish of by drawing a line up to position (0,0) */ 
  100. };
  101.  
  102. /* The Border structure: */
  103. struct Border ok_border=
  104. {
  105.   0, 0,        /* LeftEdge, TopEdge. */
  106.   1,           /* FrontPen, colour register 1. */
  107.   0,           /* BackPen, for the moment unused. */
  108.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  109.   5,           /* Count, 5 pair of coordinates in the array. */
  110.   ok_border_points, /* XY, pointer to the array with the coord. */
  111.   NULL,        /* NextBorder, no other Border structures are connected. */
  112. };
  113.  
  114. /* The IntuiText structure: */
  115. struct IntuiText ok_text=
  116. {
  117.   1,      /* FrontPen, colour register 1. */
  118.   0,      /* BackPen, not used since JAM1. */
  119.   JAM1,   /* DrawMode, draw the characters with colour 1, do not */
  120.           /* change the background. */ 
  121.   4, 2,   /* LeftEdge, TopEdge. */
  122.   NULL,   /* ITextFont, use default font. */
  123.   "OK",   /* IText, the text that will be printed. */
  124.   NULL,   /* NextText, no other IntuiText structures are connected. */
  125. };
  126.  
  127. struct Gadget ok_gadget=
  128. {
  129.   &prop_gadget,  /* NextGadget, linked to the Proportional gadget. */
  130.   14,            /* LeftEdge, 14 pixels out. */
  131.   47,            /* TopEdge, 47 lines down. */
  132.   23,            /* Width, 23 pixels wide. */
  133.   11,            /* Height, 11 pixels lines heigh. */
  134.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  135.                  /* will be rendered in the complement colours. */
  136.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  137.                  /* (Colour 1 (01)           - " -           2 (10) */
  138.                  /* (Colour 2 (10)           - " -           1 (01) */
  139.                  /* (Colour 3 (11)           - " -           0 (00) */  
  140.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  141.   RELVERIFY|     /* the user has selected this gadget, and when the user */
  142.                  /* has released it. */
  143.   ENDGADGET,     /* When the user has selected this gadget, the */
  144.                  /* requester is satisfied, and is deactivated. */
  145.                  /* IMPORTANT! At least one gadget per requester */
  146.                  /* must have the flag ENDGADGET set. If not, the */
  147.                  /* requester would never be deactivated! */
  148.  
  149.   BOOLGADGET|    /* GadgetType, a Boolean gadget which is connected to */
  150.   REQGADGET,     /* a requester. IMPORTANT! Every gadget which is */
  151.                  /* connectd to a requester must have the REQGADGET flsg */
  152.                  /* set in the GadgetType field. */
  153.   (APTR) &ok_border, /* GadgetRender, a pointer to our Border struc. */
  154.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  155.                  /* with an alternative image. (We complement the */
  156.                  /* colours instead) */
  157.   &ok_text,      /* GadgetText, a pointer to our IntuiText structure. */
  158.                  /* (See chapter 3 GRAPHICS for more information) */
  159.   NULL,          /* MutualExclude, no mutual exclude. */
  160.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  161.                  /* (No binary mask) */
  162.   0,             /* GadgetID, no id. */
  163.   NULL           /* UserData, no user data connected to the gadget. */
  164. };
  165.  
  166.  
  167.  
  168. /***********************************/
  169. /* THE CANCEL GADGET's STRUCTURES: */
  170. /***********************************/
  171.  
  172. /* The coordinates for the CANCEL box: */
  173. SHORT cancel_border_points[]=
  174. {
  175.    0,  0, /* Start at position (0,0) */
  176.   54,  0, /* Draw a line to the right to position (54,0) */
  177.   54, 10, /* Draw a line down to position (54,10) */
  178.    0, 10, /* Draw a line to the left to position (0,10) */
  179.    0,  0  /* Finish of by drawing a line up to position (0,0) */ 
  180. };
  181.  
  182. /* The Border structure: */
  183. struct Border cancel_border=
  184. {
  185.   0, 0,        /* LeftEdge, TopEdge. */
  186.   1,           /* FrontPen, colour register 1. */
  187.   0,           /* BackPen, for the moment unused. */
  188.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  189.   5,           /* Count, 5 pair of coordinates in the array. */
  190.   cancel_border_points, /* XY, pointer to the array with the coord. */
  191.   NULL,        /* NextBorder, no other Border structures are connected. */
  192. };
  193.  
  194. /* The IntuiText structure: */
  195. struct IntuiText cancel_text=
  196. {
  197.   1,        /* FrontPen, colour register 1. */
  198.   0,        /* BackPen, not used since JAM1. */
  199.   JAM1,     /* DrawMode, draw the characters with colour 1, do not */
  200.             /* change the background. */ 
  201.   4, 2,     /* LeftEdge, TopEdge. */
  202.   NULL,     /* ITextFont, use default font. */
  203.   "CANCEL", /* IText, the text that will be printed. */
  204.   NULL,     /* NextText, no other IntuiText structures are connected. */
  205. };
  206.  
  207. struct Gadget cancel_gadget=
  208. {
  209.   &ok_gadget,    /* NextGadget, linked to the OK gadget. */
  210.   214,           /* LeftEdge, 214 pixels out. */
  211.   47,            /* TopEdge, 47 lines down. */
  212.   55,            /* Width, 55 pixels wide. */
  213.   11,            /* Height, 11 pixels lines heigh. */
  214.   GADGHCOMP,     /* Flags, when this gadget is highlighted, the gadget */
  215.                  /* will be rendered in the complement colours. */
  216.                  /* (Colour 0 (00) will be changed to colour 3 (11) */
  217.                  /* (Colour 1 (01)           - " -           2 (10) */
  218.                  /* (Colour 2 (10)           - " -           1 (01) */
  219.                  /* (Colour 3 (11)           - " -           0 (00) */  
  220.   GADGIMMEDIATE| /* Activation, our program will recieve a message when */
  221.   RELVERIFY|     /* the user has selected this gadget, and when the user */
  222.                  /* has released it. */
  223.   ENDGADGET,     /* When the user has selected this gadget, the */
  224.                  /* requester is satisfied, and is deactivated. */
  225.                  /* IMPORTANT! At least one gadget per requester */
  226.                  /* must have the flag ENDGADGET set. If not, the */
  227.                  /* requester would never be deactivated! */
  228.  
  229.   BOOLGADGET|    /* GadgetType, a Boolean gadget which is connected to */
  230.   REQGADGET,     /* a requester. IMPORTANT! Every gadget which is */
  231.                  /* connectd to a requester must have the REQGADGET flsg */
  232.                  /* set in the GadgetType field. */
  233.   (APTR) &cancel_border, /* GadgetRender, a pointer to our Border struc. */
  234.   NULL,          /* SelectRender, NULL since we do not supply the gadget */
  235.                  /* with an alternative image. (We complement the */
  236.                  /* colours instead) */
  237.   &cancel_text,  /* GadgetText, a pointer to our IntuiText structure. */
  238.                  /* (See chapter 3 GRAPHICS for more information) */
  239.   NULL,          /* MutualExclude, no mutual exclude. */
  240.   NULL,          /* SpecialInfo, NULL since this is a Boolean gadget. */
  241.                  /* (No binary mask) */
  242.   0,             /* GadgetID, no id. */
  243.   NULL           /* UserData, no user data connected to the gadget. */
  244. };
  245.  
  246.  
  247.  
  248. /************************************************************************/
  249. /* Note:                                                                */
  250. /* Remember that every gadget which is connected to a requester must    */
  251. /* have the flag REQGADGET set in the GadgetType field. Remember also   */
  252. /* that at least one gadget per requester must have the ENDGADGET flag  */
  253. /* set in the Activation field.                                         */
  254. /* In this example we have three gadgets connected to the requester.    */
  255. /* All of them has the REQGADGET flag set, and the OK and CANCEL gadget */
  256. /* has also the ENDGADGET flag set.                                     */
  257. /************************************************************************/
  258.  
  259.  
  260.  
  261. /************************************/
  262. /* THE BORDER AROUND THE REQUESTER: */
  263. /************************************/
  264.  
  265. /* The coordinates for the box around the requester: */
  266. SHORT requester_border_points[]=
  267. {
  268.     0,  0, /* Start at position (0,0) */
  269.   282,  0, /* Draw a line to the right. */
  270.   282, 64, /* Draw a line down. */
  271.     0, 64, /* Draw a line to the left. */
  272.     0,  0  /* Finish of by drawing a line up to position (0,0) */ 
  273. };
  274.  
  275. /* The Border structure for the requester: */
  276. struct Border requester_border=
  277. {
  278.   0, 0,        /* LeftEdge, TopEdge. */
  279.   1,           /* FrontPen, colour register 1. */
  280.   0,           /* BackPen, for the moment unused. */
  281.   JAM1,        /* DrawMode, draw the lines with colour 1. */
  282.   5,           /* Count, 5 pair of coordinates in the array. */
  283.   requester_border_points, /* XY, pointer to the array with the coord. */
  284.   NULL,        /* NextBorder, no other Border structures are connected. */
  285. };
  286.  
  287.  
  288.  
  289. /**********************************/
  290. /* THE TEXT INSIDE THE REQUESTER: */
  291. /**********************************/
  292.  
  293. /* The IntuiText structure used to print some text inside the requester: */
  294. struct IntuiText requester_text=
  295. {
  296.   1,         /* FrontPen, colour register 1. */
  297.   0,         /* BackPen, unused since JAM1. */
  298.   JAM1,      /* DrawMode, draw the characters with colour 1, do not */
  299.              /* change the background. */ 
  300.   14, 8,     /* LeftEdge, TopEdge. */
  301.   NULL,      /* ITextFont, use default font. */
  302.   "Please set the colour value:", /* IText, the text. */
  303.   NULL,      /* NextText, no other IntuiText structures are connected. */
  304. };
  305.  
  306.  
  307.  
  308. /****************************/
  309. /* THE REQUESTER STRUCTURE: */
  310. /****************************/
  311.  
  312. struct Requester my_requester=
  313. {
  314.   NULL,              /* OlderRequester, used by Intuition. */
  315.   40, 20,            /* LeftEdge, TopEdge, 40 pixels out, 20 lines down. */
  316.   283, 65,           /* Width, Height, 283 pixels wide, 65 lines high. */
  317.   0, 0,              /* RelLeft, RelTop, Since POINTREL flag is not set, */
  318.                      /* Intuition ignores these values. */
  319.   &cancel_gadget,    /* ReqGadget, pointer to the first gadget. */
  320.   &requester_border, /* ReqBorder, pointer to a Border structure. */
  321.   &requester_text,   /* ReqText, pointer to a IntuiText structure. */
  322.   NULL,              /* Flags, no flags set. */
  323.   2,                 /* BackFill, draw everything on a black background. */
  324.   NULL,              /* ReqLayer, used by Intuition. Set to NULL. */
  325.   NULL,              /* ReqPad1, used by Intuition. Set to NULL. */
  326.   NULL,              /* ImageBMap, no predrawn Bitmap. Set to NULL. */
  327.                      /*            (The PREDRAWN flag was not set) */
  328.   NULL,              /* RWindow, used by Intuition. Set to NULL. */
  329.   NULL               /* ReqPad2, used by Intuition. Set to NULL. */
  330. };
  331.  
  332.  
  333.  
  334. /* Declare a pointer to a Window structure: */ 
  335. struct Window *my_window;
  336.  
  337. /* Declare and initialize your NewWindow structure: */
  338. struct NewWindow my_new_window=
  339. {
  340.   0,             /* LeftEdge    x position of the window. */
  341.   0,             /* TopEdge     y positio of the window. */
  342.   640,           /* Width       640 pixels wide. */
  343.   200,           /* Height      200 lines high. */
  344.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  345.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  346.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  347.                  /*             user has selected the Close window gad, */
  348.   GADGETDOWN|    /*             or a gadget has been pressed on, or */
  349.   GADGETUP|      /*             a gadge has been released. */
  350.   REQSET|        /*             Send a message also if a requester has */
  351.   REQCLEAR,      /*             been activated or deactivated. */
  352.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  353.   WINDOWCLOSE|   /*             Close Gadget. */
  354.   WINDOWDRAG|    /*             Drag gadget. */
  355.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  356.   WINDOWSIZING|  /*             Sizing Gadget. */
  357.   ACTIVATE,      /*             The window should be Active when opened. */
  358.   NULL,          /* FirstGadget No gadget connected to this window. */
  359.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  360.   "The Window",  /* Title       Title of the window. */
  361.   NULL,          /* Screen      Connected to the Workbench Screen. */
  362.   NULL,          /* BitMap      No Custom BitMap. */
  363.   140,           /* MinWidth    We will not allow the window to become */
  364.   50,            /* MinHeight   smaller than 140 x 50, and not bigger */
  365.   300,           /* MaxWidth    than 300 x 200. */
  366.   200,           /* MaxHeight */
  367.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  368. };
  369.  
  370.  
  371.  
  372. main()
  373. {
  374.   /* Boolean variable used for the while loop: */
  375.   BOOL close_me;
  376.  
  377.   /* Declare a variable in which we will store the IDCMP flag: */
  378.   ULONG class;
  379.   
  380.   /* Declare a variable in which we will store the address of the */
  381.   /* gadget which sent the message: */
  382.   APTR address;
  383.   
  384.   /* Declare a pointer to an IntuiMessage structure: */
  385.   struct IntuiMessage *my_message;
  386.  
  387.   /* We use this variable to check if the requester has ben activated */
  388.   /* or not: */
  389.   BOOL result;
  390.  
  391.  
  392.  
  393.   /* Before we can use Intuition we need to open the Intuition Library: */
  394.   IntuitionBase = (struct IntuitionBase *)
  395.     OpenLibrary( "intuition.library", 0 );
  396.   
  397.   if( IntuitionBase == NULL )
  398.     exit(); /* Could NOT open the Intuition Library! */
  399.  
  400.  
  401.  
  402.   /* We will now try to open the window: */
  403.   my_window = (struct Window *) OpenWindow( &my_new_window );
  404.   
  405.   /* Have we opened the window succesfully? */
  406.   if(my_window == NULL)
  407.   {
  408.     /* Could NOT open the Window! */
  409.     
  410.     /* Close the Intuition Library since we have opened it: */
  411.     CloseLibrary( IntuitionBase );
  412.  
  413.     exit();  
  414.   }
  415.  
  416.  
  417.  
  418.   /* We have opened the window, and everything seems to be OK. */
  419.  
  420.  
  421.  
  422.   /* We will now try to activate the requester: */
  423.   result=Request( &my_requester, my_window );
  424.  
  425.   if( !result )  /* !result is the same thing as result==FALSE */
  426.   {
  427.     /* Intuition could not activate the requester! */
  428.     /* In this case we do not need to quit since it does not matter if */
  429.     /* the requester was activated or not. I just wanted to show how */
  430.     /* you can check if you have opened or not the requester. */
  431.   
  432.     printf("Could not activate the requester!\n");
  433.   }
  434.   else
  435.   {
  436.     /* Intuition could open the requester! */
  437.     printf("Try to close the window!\n");
  438.   }
  439.  
  440.  
  441.  
  442.   close_me = FALSE;
  443.  
  444.   /* Stay in the while loop until the user has selected the Close window */
  445.   /* gadget. However, in this example the user first need to deactivate */
  446.   /* the requester before he can select the Close window gadget: */
  447.   while( !close_me )
  448.   {
  449.     /* Wait until we have recieved a message: */
  450.     Wait( 1 << my_window->UserPort->mp_SigBit );
  451.  
  452.     /* As long as we collect messages sucessfully: */
  453.     while(my_message=(struct IntuiMessage *) GetMsg(my_window->UserPort))
  454.     {
  455.       /* After we have collected the message we can read it, and save any */
  456.       /* important values which we maybe want to check later: */
  457.       
  458.       /* Store the IDCMP flag: */
  459.       class = my_message->Class;
  460.  
  461.       /* Store the address: */
  462.       address = my_message->IAddress;
  463.  
  464.       /* After we have read it we reply as fast as possible: */
  465.       /* REMEMBER! Do never try to read a message after you have replied! */
  466.       /* Some other process has maybe changed it. */
  467.       ReplyMsg( my_message );
  468.  
  469.       /* Check which IDCMP flag was sent: */
  470.       switch( class )
  471.       {
  472.         case CLOSEWINDOW:  /* The user selected the Close window gadget! */
  473.                close_me=TRUE;
  474.                break;
  475.              
  476.         case GADGETDOWN:   /* The user has pressed on a gadget. */
  477.                
  478.                if( address == (APTR) &ok_gadget )
  479.                  printf("The user pressed on the OK gadget!\n");
  480.  
  481.                if( address == (APTR) &cancel_gadget )
  482.                  printf("The user pressed on the CANCEL gadget!\n");
  483.                  
  484.                if( address == (APTR) &prop_gadget )
  485.                  printf("The user selected the Proportional gadget!\n");
  486.                
  487.                break;
  488.              
  489.         case GADGETUP:     /* The user has released a gadget. */
  490.  
  491.                if( address == (APTR) &ok_gadget )
  492.                  printf("The user released the OK gadget!\n");
  493.  
  494.                if( address == (APTR) &cancel_gadget )
  495.                  printf("The user released the CANCEL gadget!\n");
  496.                  
  497.                if( address == (APTR) &prop_gadget )
  498.                {
  499.                  printf("The user released the Proportional gadget!\n");
  500.                  
  501.                  /* Print out the colour value: */
  502.                  printf("Colour= %1.0f\n\n", (float) prop_info.HorizPot
  503.                                              / MAXPOT*16);
  504.                }
  505.                break;
  506.                
  507.         case REQSET:       /* Requester activated. */
  508.               printf("Requester activated!\n");
  509.               break;
  510.  
  511.         case REQCLEAR:     /* Requester deactivated. */
  512.               printf("Requester deactivated!\n");
  513.               printf("You can now close the window.\n");
  514.               break;
  515.       }
  516.     }
  517.   }
  518.  
  519.  
  520.  
  521.   /* Print out the colour value: */
  522.   printf( "Colour= %1.0f\n\n", (float) prop_info.HorizPot / MAXPOT*16 );
  523.  
  524.  
  525.  
  526.   /* We should always close the windows we have opened before we leave: */
  527.   CloseWindow( my_window );
  528.  
  529.  
  530.  
  531.   /* Close the Intuition Library since we have opened it: */
  532.   CloseLibrary( IntuitionBase );
  533.   
  534.   /* THE END */
  535. }